package org.study.es.boot.service;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.stereotype.Service;
import org.study.es.boot.entity.BookEntity;
import org.study.es.boot.repository.BookRepository;

import java.util.List;
import java.util.Optional;

/**
 * 类的描述
 *
 * @author Administrator
 * @date 2021-12-31
 */
@Service("blogService")
public class BookServiceImpl implements BookService {
    @Autowired
    private BookRepository bookRepository;
    @Resource
    private ElasticsearchTemplate elasticsearchTemplate;

    @Override
    public BookEntity findById(String id) {
        BookEntity bookEntity = null;
        Optional<BookEntity> optional = bookRepository.findById(id);
        if (optional.isPresent()) {
            bookEntity = optional.get();
        }
        return bookEntity;
    }

    @Override
    public BookEntity save(BookEntity blog) {
        return bookRepository.save(blog);
    }

    @Override
    public void delete(BookEntity blog) {
        bookRepository.delete(blog);
    }

    @Override
    public List<BookEntity> findAll() {
        return (List<BookEntity>) bookRepository.findAll();
    }

    @Override
    public Page<BookEntity> findByAuthor(String author, PageRequest pageRequest) {
        return bookRepository.findByAuthor(author, pageRequest);
    }

    @Override
    public Page<BookEntity> findByTitle(String title, PageRequest pageRequest) {
        return bookRepository.findByTitle(title, pageRequest);
    }
}
